<?php
namespace app\mobile\controller;

use app\common\controller\Base;
use app\common\model\GoodsModel;
use app\common\model\MemberModel;
use app\common\model\PaymentModel;
use app\common\service\GoodsSpecService;
use app\common\service\MemberService;
use common\util\UrlUtil;
use web\util\WebUtil;
use web\util\WechatUtil;

class Index extends Base {

    /*
     * 初始化操作
     */
    public function _initialize() {
        parent::_initialize();

        //不需要登录白名单函数
        $writeFuns = [
            'login',
            'register',
            'goods',
            'goods_detail'
        ];
        $url = $this->request->request('s');
        $fun = getCurlFunc($url);
        if(!in_array($fun, $writeFuns)){
            $this->needLogin();
        }
    }
    
    /**
     * 注册
     */
    public function register() {
        $mid = $this->getLoginId();
        if($mid){
            return $this->redirect('/mobile/index/home.html');
        }
        
        return $this->fetch();
    }

    /**
     * 登陆
     */
    public function login() {
        return $this->fetch();
    }

    /**
     * 退出
     */
    public function logout() {
        $this->clearLogin();
        $this->success("退出成功", 'Index/home');
    }

    /**
     * 主页
     */
    public function home() {
        return $this->fetch();
    }

    /**
     * 个人资料
     */
    public function profile() {
        return $this->fetch();
    }

    /**
     * 绑定微信
     */
    public function bind_wechat() {
        $mid = $this->getLoginId();
        $member = MemberModel::instance()-> getInfoById($mid);
        $passportUid = $member['passport_uid'];
        $callbackUrl = UrlUtil::getHost()."/mobile/index/bind_callback";
        $url = WechatUtil::getWeixinAuthUrl($callbackUrl);
        $this->assign('url',$url);
        $this->assign('has_bind',!empty($passportUid));
        return $this->fetch();
    }
    
    /**
     * 绑定回调
     */
    public function bind_callback(){
        $passportUid = WechatUtil::getPassportUid();
        if($passportUid){
            $mid = $this->getLoginId();
            $user = WechatUtil::getPassportUser();
            $avatar = $user['avatar'];
            $member = MemberModel::instance()->getInfoById($mid);
            $result = false;
            $msg = '';
            if($member['passport_uid']){//已经绑定
                $msg = '已经绑定';
                $result = false;
            }else{
                $data = [
                    'id'=>$mid,
                    'passport_uid'=>$passportUid,
                    'bind_time'=>time(),
                    'openid' => $user['openids'][get_appid()],
                ];
                if(empty($member['face'])){//没有设置头像
                    $data['face'] = $avatar;
                }
                MemberModel::instance()->bindWeixin($data);
                $msg = '绑定成功';
                $result = true;
            }
        }
        $this->assign('result',$result);
        $this->assign('msg',$msg);
        return $this->fetch();
    }
    
    /**
     * 移除绑定
     */
    public function unbind(){
        $mid = $this->getLoginId();
        $data = [
            'id'=>$mid,
            'passport_uid'=>0,
            'openid'=>'',
            'bind_time'=>time(),
        ];
        MemberModel::instance()->update($data);
        return $this->returnResult(1,'移除成功');
    }

    /**
     * 修改密码
     */
    public function password() {
        return $this->fetch();
    }

    /**
     * 收货地址管理
     */
    public function address() {
        return $this->fetch();
    }

    /**
     * 下单选择收货地址
     */
    public function address_list() {
        return $this->fetch();
    }

    /**
     * 收货地址管理 | 新增
     */
    public function address_add() {
        return $this->fetch();
    }

    /**
     * 收货地址管理 | 编辑
     */
    public function address_edit() {
        return $this->fetch();
    }


    /**
     * 发货地址管理
     */
    public function address_start_list()
    {
        return $this->fetch();
    }

    /**
     * 发货地址管理
     */
    public function address_start()
    {
        return $this->fetch();
    }

    /**
     * 发货地址管理 | 新增
     */
    public function address_start_add()
    {
        return $this->fetch();
    }

    /**
     * 发货地址管理 | 编辑
     */
    public function address_start_edit()
    {
        return $this->fetch();
    }

    /**
     * 银行卡管理
     */
    public function bankcard() {
        return $this->fetch();
    }

    /**
     * 银行卡管理 | 新增
     */
    public function bankcard_add() {
        return $this->fetch();
    }

    /**
     * 银行卡管理 | 编辑
     */
    public function bankcard_edit() {
        return $this->fetch();
    }

    /**
     * 钱包管理
     */
    public function assets() {
        return $this->fetch();
    }

    /**
     * 充值
     */
    public function recharge() {
        return $this->fetch();
    }

    /**
     * 充值 | 列表
     */
    public function recharge_list() {
        return $this->fetch();
    }

    /**
     * 充值 | 详情
     */
    public function recharge_detail() {
        return $this->fetch();
    }

    /**
     * 提现
     */
    public function withdraw() {
        
        $mid = $this->getLoginId();
        $member = MemberModel::instance()->where('id',$mid)->find();
        $rewardaccount = $member['rewardaccount'];
        $this->assign('rewardaccount',$rewardaccount);
        $payment = PaymentModel::instance()->where(['mid'=>$mid,'ifdefault'=>1])->find();
        if(empty($payment)){
            $payment = ['id'=>0,'paytype_name'=>'','num'=>''];
        }else{
            $payment['paytype_name'] = WebUtil::getPaymentTypeName($payment['paytype']);
        }
        $this->assign('payment',$payment);
        return $this->fetch();
    }
    
    /**
     * 提现 | 列表
     */
    public function withdraw_list() {
        return $this->fetch();
    }

    /**
     * 提现 | 详情
     */
    public function withdraw_detail() {
        return $this->fetch();
    }

    /**
     * 业绩 | 团队
     */
    public function achievement() {
        $date = date('Y/m/d');
        $this->assign('date',$date);
        return $this->fetch();
    }

    /**
     * 业绩 | 团队
     */
    public function achievement_team() {
        $date = date('Y/m/d');
        $this->assign('date',$date);
        return $this->fetch();
    }

    /**
     * 业绩 | 个人
     */
    public function achievement_alone() {
        $date = date('Y/m/d');
        $this->assign('date',$date);
        return $this->fetch();
    }

    /**
     * 业绩 | 团队充值金额
     */
    public function achievement_recharge() {
        $date = date('Y/m/d');
        $this->assign('date',$date);
        return $this->fetch();
    }


    /**
     * 订单奖励
     */
    public function reward_order() {
        $mid = $this->getLoginId();
        $member = MemberModel::instance()->find($mid);
        $this->assign('grade',$member['grade']);
        
        $date = date('Y/m/d');
        $this->assign('date',$date);
        return $this->fetch();
    }

    /**
     * 团队奖励
     */
    public function reward_team() {
        
        $mid = $this->getLoginId();
        $member = MemberModel::instance()->find($mid);
        $this->assign('grade',$member['grade']);
        $this->assign('year',date('Y年'));
        $this->assign('month',date('m月'));
        
        return $this->fetch();
    }

    /**
     * 领导积分奖励
     */
    public function reward_leader() {
        
        $mid = $this->getLoginId();
        $member = MemberModel::instance()->find($mid);
        $this->assign('grade',$member['grade']);
        $this->assign('year',date('Y年'));
        $this->assign('month',date('m月'));
        return $this->fetch();
    }

    /**
     * 直属代理
     */
    public function agent_team() {
        $date = date('Y/m/d');
        $this->assign('date',$date);
        return $this->fetch();
    }

    /**
     * 分销代理
     */
    public function agent_personal() {
        return $this->fetch();
    }
        

    /**
     * 邀请
     */
    public function invite() {
        return $this->fetch();
    }

    /**
     * 商品列表
     */
    public function goods() {
        return $this->fetch();
    }

    /**
     * 商品详情
     */
    public function goods_detail() {
        $id = $this->request->param('id');
        $goods = GoodsModel::instance()->where('isshow',1)->find($id);
        
        $pics = [];
        
        $multipic = explode(',', $goods['multipic']);
        foreach ($multipic as &$pic){
            $pics[] = get_image($pic,'!750x400');
        }
        
        //规格列表 
        //规格列表
        $goods_type_id= $goods['goods_type_id'];
        $goodsSpecs = [];
        if($goods_type_id){
            $goodsSpecs = GoodsSpecService::getGoodsSpecList($goods_type_id,false);
        }
        $this->assign('pics',$pics);
        $this->assign('goods_specs',$goodsSpecs);
        $this->assign('pics_count',count($pics));
        return $this->fetch();
    }

    /**
     * 订单提交
     */
    public function order_submit() {
        return $this->fetch();
    }

    /**
     * 订单列表
     */
    public function order() {
        $status = $this->request->param('id',0);
        $this->assign('status',$status);
        return $this->fetch();
    }
    /**
     * 订单列表
     */
    public function order_detail() {
        return $this->fetch();
    }
    
    public function agent_sub(){
        $this->needLogin();
        $mid = $this->getLoginId();
        $sub_id  = $this->request->param('sub_id');
        
        $memberModel = new \app\common\model\MemberModel();
        $subMember = $memberModel->find($sub_id);
        $pid_path = $subMember['pid_path'];
        $pids = explode(',',$pid_path);
        
        if(!in_array($mid, $pids)){
            return $this->redirect('/');
        }
        $list = MemberService::getSubList($sub_id);
        $data = [
            'id'=>$sub_id,
            'avatar'=>get_image($subMember['face']),
            'name'=>$subMember['name'],
            'grade_name'=>WebUtil::getGradeName($subMember['grade']),
            'sub_list'=>$list,
        ];
        
        $this->assign($data);
        return $this->fetch();
    }
}
